#!/bin/bash

home="RM2-Components"
home_path="../"
temp_prefix="SimTemp"
results_dir="SimResults"

parallel_sim=5

mkdir "$home_path""$results_dir"

while IFS=, read nodes noise run
do
	mkdir "$home_path""$results_dir""/$noise""-$nodes"
	div=$(($run/$parallel_sim))
	mod=$(($run%$parallel_sim))
	
	id=1
	
	for ((r=1 ; $r<=$parallel_sim ; r++)); do
		cp -r "$home_path$home" "$home_path""$temp_prefix""$r""-""$home"
		cd "$home_path""$temp_prefix""$r""-""$home"
		make clean
		make micaz sim
		if [ $r -le $mod ]; then
			./RunSingleSim.sh $((div+1)) $r $id $noise $nodes &
			id=$(($id+$div+1))
		else
			./RunSingleSim.sh $div $r $id $noise $nodes &
			id=$(($id+$div))
		fi
		
	done
	wait
#	let count=`ps aux | grep -c "/bin/bash ./RunSingleSim"`
#	while [ $count -gt 1 ]
#	do
#		#echo "Waiting for processes to finish...($count)"
#		sleep 1
#		let count=`ps aux | grep -c "/bin/bash ./RunSingleSim"`
#	done
	
done < "simulation.conf"

exit	
	
	
#	cp -r "$home_path$home" "$home_path""$temp_prefix""$r""-""$home"
#	cd "$home_path""$temp_prefix""$r""-""$home"
#	make clean
#	make micaz sim
#	./RunSingleSim.sh &
#done



#cp -r ../RuinsMonitor-2.0.0 ../Temp1-RuinsMonitor-2.0.0
#cd ../Temp1-RuinsMonitor-2.0.0
#make clean
#make micaz sim
#cd ../RuinsMonitor-2.0.0
